package com.czwj.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.CameraUser;
import com.czwj.entities.Camera;
import com.czwj.entities.CameraTime;
import com.czwj.entities.CameraUser;
import com.czwj.entities.UserInfo;
import com.czwj.service.CameraService;
import com.czwj.service.CameraTimeService;
import com.czwj.service.CameraUserService;
import com.czwj.util.TextUtil;

/**
* @ClassName: CameraAction
* @Description: TODO
* @author H
* @date 2020年5月19日
*
*/
@Controller
public class CameraAction{
	@Resource
	CameraService cameraService;
	
	@Resource
	CameraUserService cameraUserService;
	
	@Resource
	CameraTimeService cameraTimeService;
	
	@RequestMapping(value = "cameras",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Camera> cameras(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) String projectitemchildid){
		DataGridData<Camera> result = new DataGridData<>();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)){
			map.put("projectitemchildid",projectitemchildid);
		}
		List<Camera> list = cameraService.search(map);
		result.setRows(list);
		result.setTotal(cameraService.count(map));
		return result;
	}
	

	@RequestMapping(value = "cameraInfo",method = RequestMethod.GET)
	@ResponseBody
	public Camera cameraInfo(@RequestParam(value = "id",required = true) Integer id){
		Camera camera = cameraService.selectByPrimaryKey(id);
		return camera;
	}
	
	@RequestMapping(value = "camerauser",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Camera> camerauser(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "projectitemid",required = false) String projectitemid,
			@RequestParam(value = "projectitemchildid",required = false) String projectitemchildid){
		DataGridData<Camera> result = new DataGridData<>();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(projectitemid)){
			map.put("projectitemid",projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)){
			map.put("projectitemchildid",projectitemchildid);
		}
		Subject currentUser = SecurityUtils.getSubject();
		int userid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId();
		map.put("userid",userid);
		List<Camera> list = cameraService.searchforuser(map);
		result.setRows(list);
		result.setTotal(cameraService.countforuser(map));
		return result;
	}
	
	@SystemControllerLog(description = "修改摄像头")
	@RequestMapping(value = "/CameraSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult CameraSave(@RequestBody Camera item,HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(item.getId() != null && item.getId() > 0){
			count = cameraService.update(item);
		}else{
			count = cameraService.insert(item);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除摄像头")
	@RequestMapping(value = "CameraDelete.do",method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult CameraDelete(@RequestParam(value = "id",required = true) Integer id){
		OperateResult result = new OperateResult();
		int count = cameraService.delete(id);
		result.setSuccess(count > 0);
		return result;
	}
	
	/**
	* @Title: setCameraUsers
	* @Description: 更新权限
	* @param params
	* @return 参数 
	* @return OperateResult 返回类型
	* @throws
	*/
	@SystemControllerLog(description = "修改摄像头权限")
	@RequestMapping(value = "cameraUserSave.do",method = RequestMethod.PUT)
	@ResponseBody
	public OperateResult cameraUserSave(@RequestBody Map<String,Object> params){
		OperateResult result = new OperateResult();
		int cameraid = Integer.valueOf(params.get("cameraid").toString());
		int userid = Integer.valueOf(params.get("userid").toString());
		boolean check = Boolean.valueOf(params.get("check").toString());
		Map<String,Object> rpParams = new HashMap<>();
		rpParams.put("cameraid",cameraid);
		rpParams.put("userid",userid);
		List<CameraUser> rps = cameraUserService.search(rpParams);
		int count = 0;
		if(check){
			if(rps.size() <= 0){
				CameraUser levelPer = new CameraUser();
				levelPer.setUserid(userid);
				levelPer.setCameraid(cameraid);
				count = cameraUserService.insert(levelPer);
			}
		}else{
			if(rps.size() > 0){
				for(CameraUser rp:rps){
					count += cameraUserService.delete(rp.getId());
				}
			}
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@RequestMapping(value = "cameraTimes",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<CameraTime> cameraTimes(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<CameraTime> result = new DataGridData<>();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		List<CameraTime> list = cameraTimeService.search();
		result.setRows(list);
		result.setTotal(1);
		return result;
	}

	@SystemControllerLog(description = "修改摄像头时间配置")
	@RequestMapping(value = "/CameraTimeSave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult CameraTimeSave(@RequestBody CameraTime item){
		OperateResult result = new OperateResult();
		int count = cameraTimeService.update(item);
		result.setSuccess(count > 0);
		return result;
	}
}
